<?php
namespace Home\Controller;
use Home\Controller\BaseController;
class HotelsController extends BaseController {
    private $perpage = 6;
    private $M;
    public function _initialize(){
        $this->M = M("Hotels");
    }
    public function details(){
        $this->display();
    }
    public function index(){
        $da = M("Hotels") -> select();
        $this -> assign("hotels",$da);
        echo R("Navlink/index");

        $ada = M("area");
        $data = $ada->create();
        if(empty($data["id"])){
            $res = $ada->add();
        }
        else{
            $res = $ada->save();
        }
		$count = $this->M->count();    // 总记录数
        $pages = ceil($count / $this->perpage);
        if(empty($nowpage)){
            $nowpage = 1;
        }
        $Page = new \Think\Page($count, $this->perpage);
        $Page->setConfig("prev", "上一页");
        $Page->setConfig("next", "下一页");
        $this->M->alias("h")->join(array(
            "left JOIN y_area a on a.id=h.province",
            "left join y_area b on b.id=h.city",
            "LEFT JOIN y_area c on c.id=h.district",
            "left join __SENIC__ s on s.id=h.senic_id",
            "left join __HOTELS_PIC__ t on t.hotels_id=h.id"
            ))->field(array(
            "h.*",
            "a.name"=> "province",
            "b.name"=> "city",
            "c.name"=> "district",
            "s.name"=> "senic",
            "t.picture"=> "picture"
        ));
        $list = $this->M->limit($Page->firstRow.','.$Page->listRows)->select();   // 每页的数据数组
        $show = $Page->show();  // 页码链接内容
        $this->assign("count", $count);
        $this->assign("list", $list);
        $this->assign("page", $show);
        $this->assign("pages", $pages);
        $this->assign("nowpage", $nowpage);
        $this->display();
    }
    public function hotels(){
        echo R("Navlink/index");
        $rm = M("rooms");
        $id = I("id");
        if($id==""){
            $this->redirect("/Home/Hotels",array(),0,"");
        }
        $rm->alias("r")->join(array(
            "left JOIN __HOTELS__ h on r.hotels_id=h.id",
            "left join __ROOMS_PIC__ p on p.rooms_id=r.id"
        ))->field(array(
            "r.*",
            "h.name"=> "name",
            "p.picture"=> "picture"
        ));
        
        
        $list =$rm->where("h.id=".$id)->find();
        $this->assign("list",$list);
        $this->display();
    }
}